﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Tickettoticket;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.InvoicesandCommand;
using NPOI.SS.Formula.Functions;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.InvoicesandCommandHandler
{
    /// <summary>
    /// 开票到票采购订单
    /// 物联网-智能MES生产管理系统-07-开票到票管理V1.1-20250206
    /// </summary>
    public class updateicetsCommandHander : IRequestHandler<updateicetsCommand, APIResult<object>>
    {

        /// <summary>
        /// 开票到票
        /// </summary>
        private readonly IBaseRepository<Billingder> billingderRepository;

        /// <summary>
        /// 销售订单
        /// </summary>
        private readonly IBaseRepository<urchaseOrdp> urchaseOrdpRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        private readonly SDbContext db;

        public updateicetsCommandHander(IBaseRepository<Billingder> billingderRepository, IBaseRepository<urchaseOrdp> urchaseOrdpRepository, IMapper mapper, SDbContext db)
        {
            this.billingderRepository = billingderRepository;
            this.urchaseOrdpRepository = urchaseOrdpRepository;
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public  Task<APIResult<object>> Handle(updateicetsCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var add = billingderRepository.GetAll().Result.Where(x => x.Bid == request.Bid).FirstOrDefault();
            add.Bid = request.Bid;
            add.BillingStae = request.BillingStae;
            add.Billingtopic = request.Billingtopic;
            add.Billingtype = request.Billingtype;
            add.BillingDate = request.BillingDate;
            add.Billingsale = (bool)request.Billingsale;
            add.BillTicket = request.BillTicket;
            add.number = request.number;
            add.remark = request.remark;

            int ane = billingderRepository.Update(add).Result;
            if (ane > 0)
            {
                var li = urchaseOrdpRepository.GetAll().Result.Where(X => X.UrcStae == request.BillingStae).FirstOrDefault();
                foreach (var item in request.urchaseOrdps)
                {
                    li.Urcid = item.Urcid;
                    li.UrcStae = item.UrcStae;
                    li.Urcic = item.Urcic;
                    li.UrcName = item.UrcName;
                    li.UrcTime = item.UrcTime;
                    li.Urcamount = item.Urcamount;
                    li.Urceamount = item.Urceamount;
                    li.Urcinvoice = item.Urcinvoice;
                    li.Urcremark = item.Urcremark;
                }
                var q = urchaseOrdpRepository.Update(li).Result;
                if(q > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = "更改成功";
                    result.data = q;
                    return Task.FromResult(result);
                }
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = "更改失败";
                return Task.FromResult(result);
            }
            return Task.FromResult(result);
        }
    }
}
